package com.instalinks.android.smspostservice.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.service.AppService;
import com.instalinks.android.smspostservice.utils.enums.ServiceStartStatus;

public class ServiceActivity extends AppTabBaseActivity {

	private TextView lblServiceNote;
	private CheckBox chkSMSService;
	private CheckBox chkMissedCallService;
	private CheckBox chkWebSms;
	private CheckBox chkAlphanumericSender;
	private CheckBox chkShortcodeSender;
	private CheckBox chkAutoReplySMS;
	private CheckBox chkMarkAsRead;
	
	private ToggleButton btnServiceOnOff;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initialize();
	}

	private void initialize() {
		mAppCfg.rePopulate();

		setContentView(R.layout.service);

		btnServiceOnOff = (ToggleButton) this
				.findViewById(R.id.btnServiceOnOff);

		btnServiceOnOff.setChecked(mAppCfg.isServiceRunning());

		btnServiceOnOff.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (btnServiceOnOff.isChecked()) {
					startServiceThread();
				} else {
					stopServiceThread();
				}
			}
		});

		
		//***********************************************************************//
		lblServiceNote = (TextView)findViewById(R.id.srv_lblServiceNote);
		chkSMSService = (CheckBox) findViewById(R.id.srv_chkSMSService);
		chkMissedCallService = (CheckBox) findViewById(R.id.srv_chkMissedCallService);
		
		chkAutoReplySMS = (CheckBox) findViewById(R.id.srv_chkAutoReplySMS);
		chkMarkAsRead = (CheckBox) findViewById(R.id.srv_chkMarkAsRead);
		chkWebSms = (CheckBox) findViewById(R.id.srv_chkWebSms);
		chkAlphanumericSender = (CheckBox) findViewById(R.id.srv_chkAlphanumericSender);
		chkShortcodeSender= (CheckBox) findViewById(R.id.srv_chkShortcodeSender);
		
		chkSMSService.setChecked(mAppCfg.isSmsServiceFlag());
		chkMissedCallService.setChecked(mAppCfg.isMissedServiceServiceFlag());
		chkAutoReplySMS.setChecked(mAppCfg.isAutoReplyFlag());
		chkMarkAsRead.setChecked(mAppCfg.isMarkAsReadFlag());
		chkWebSms.setChecked(mAppCfg.isWebSmsFlag());
		
		chkAlphanumericSender.setChecked(mAppCfg.isAlphanumericSenderFlag());
		chkShortcodeSender.setChecked(mAppCfg.isAlphanumericSenderFlag() || mAppCfg.isShortcodeSenderFlag());
		chkShortcodeSender.setEnabled(!mAppCfg.isAlphanumericSenderFlag());
		
		
		chkSMSService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setSmsServiceFlag(isChecked);
				} catch (Exception e) {
				}
				setMode();
			}
		});

		chkMissedCallService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setMissedServiceServiceFlag(isChecked);
				} catch (Exception e) {
				}
				setMode();
			}
		});

		chkAutoReplySMS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setAutoReplyFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

		chkMarkAsRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setMarkAsReadFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

		chkAlphanumericSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setAlphanumericSenderFlag(isChecked);
				} catch (Exception e) {
				}
				
				chkShortcodeSender.setChecked(isChecked);
				chkShortcodeSender.setEnabled(!isChecked);
			}
		});
		chkShortcodeSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setShortcodeSenderFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});
		chkWebSms.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					mAppCfg.setWebSmsFlag(isChecked);
				} catch (Exception e) {
				}
			}
		});

		setMode();
	}

	private void setMode() {
		boolean isServiceInProgress = mAppCfg.isServiceRunning();
		setMode(isServiceInProgress);
	}

	private void setMode(boolean isServiceInProgress) {

		lblServiceNote.setText(Html.fromHtml("<i>Stop the service to modify these settings</i>"));
		lblServiceNote.setVisibility( isServiceInProgress?View.VISIBLE:View.GONE);
		
		chkSMSService.setEnabled(!isServiceInProgress);
		chkMissedCallService.setEnabled(!isServiceInProgress);

		
		btnServiceOnOff.setChecked(isServiceInProgress);
		
		btnServiceOnOff.setBackgroundResource(btnServiceOnOff.isChecked()?R.drawable.toggle_on: R.drawable.toggle_off);

		btnServiceOnOff.setEnabled(mAppCfg.isSmsServiceFlag() || mAppCfg.isMissedServiceServiceFlag());
		
	}

	private void startServiceThread() {
		try {
			mAppCfg.setAvailableFreeCount(mAppCfg.getFreeCount());
		} catch (Exception e) {
		}

		ServiceActivity.this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				new SubTask("Starting Service").execute();
			}
		});
	}

	private void stopServiceThread() {
		//stopService(new Intent(this, AppService.class));
		stopService(new Intent(this, AppService.class));
		setMode(false);
	}

	private void handleServiceResponse(ServiceStartStatus status) {
		if (status != ServiceStartStatus.Started) {
			mAppCfg.handleServiceResponse(status);
		}

		boolean isStarted = (status == ServiceStartStatus.Started);
		setMode(isStarted);
	}

	private ServiceStartStatus startService() {
		mAppCfg.rePopulate();

		ServiceStartStatus status = mAppCfg.allGoodToRunService();

		if (status == ServiceStartStatus.Ok) {
			boolean result = reloadConfigFromHost();
			if (result) {
				startService(new Intent(this, AppService.class));
				return ServiceStartStatus.Started;
			} else {
				return ServiceStartStatus.IssueUpdatingConfigFile;
			}
		} else {
			return status;
		}
	}

	private ServiceStartStatus serviceStartStatus;
	
	@Override
	protected boolean doBackgroundWork() {
		try{
			serviceStartStatus = startService();
			return true;
		}catch (Exception e){
			return false;
		}
	}

	@Override
	protected void doOnSuccess() {
		handleServiceResponse(serviceStartStatus);
	}

	@Override
	protected void doOnFailure() {
		mAppCfg.alert("Error starting service. Try again");
	}
}
